import os
from dataclasses import dataclass, field
from typing import Dict


@dataclass
class PathConfig:
    """路径配置"""
    base_dir: str = field(default_factory=lambda: os.getcwd()) # 项目根目录
    docs_path: str = field(init=False) # 文档目录
    results_path: str = field(init=False) # 测试集目录
    eval_path: str = field(init=False) # 评估结果目录

    def __post_init__(self):
        self.docs_path = os.path.join(self.base_dir, "docs")
        self.results_path = os.path.join(self.base_dir, "test_sets")
        self.eval_path = os.path.join(self.base_dir, "eval_results")

@dataclass
class MaasConfig:
    """MaaS配置"""
    base_url: str = "http://10.0.6.170:9999/api/v1" # 本地MaaS服务
    generator_model: str = "Qwen2.5-14B-Chat" # 生成模型
    evaluator_model: str = "Qwen2.5-14B-Chat" # 评估模型
    embedding_model: str = "bge-large-zh-v1.5" # 嵌入模型


@dataclass
class RAGConfig:
    """RAG配置"""
    base_url: str = "http://demo.knowledge.cloudwise.com/knowledgeWeb" # 本地RAG服务
    session_id: int = 465 # 会话ID
    knowledge_id: int = 30 # 知识库ID
    scene_id: int = 4 # 场景ID


@dataclass
class TestsetConfig:
    """测试集配置"""
    size: int = 20 # 测试集大小


class Config:
    """配置类"""
    def __init__(self):
        # 环境变量
        os.environ["OPENAI_API_KEY"] = "None"
        
        # 初始化各个配置
        self.path = PathConfig()
        self.maas = MaasConfig()
        self.rag = RAGConfig()
        self.testset = TestsetConfig()
    
    def update_maas_config(self, **kwargs):
        """更新MaaS配置"""
        for key, value in kwargs.items():
            if hasattr(self.maas, key):
                setattr(self.maas, key, value)
    
    def update_rag_config(self, **kwargs):
        """更新RAG配置"""
        for key, value in kwargs.items():
            if hasattr(self.rag, key):
                setattr(self.rag, key, value)
    
    def update_testset_config(self, **kwargs):
        """更新测试集配置"""
        for key, value in kwargs.items():
            if hasattr(self.testset, key):
                setattr(self.testset, key, value)

    @property
    def rag_params(self) -> Dict[str, int]:
        """获取RAG请求参数"""
        return {
            "session_id": self.rag.session_id,
            "knowledge_id": self.rag.knowledge_id,
            "scene_id": self.rag.scene_id
        }


# 创建全局配置实例
config = Config()